#!/usr/bin/python
# -*- coding: UTF-8 -*-
'''
Created on 3 jul 2011

@author: Joakim Lindskog

Copyright (c) 2011 Joakim Lindskog

This file is part of MyGarden.

    MyGarden is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    MyGarden is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with MyGarden.  If not, see <http://www.gnu.org/licenses/>.
'''
import tool, pyglet, util, events
from cocos.director import director

class WateringCan(tool.Tool):
    '''
    A watering can to water the beds with
    '''

    def __init__(self):
        '''
        Constructor
        '''
        self.idle_image = util.load_image("watering_can_idle.png", fix=False)
        self.start_anim = util.setup_animation('watering_can_tiled.png', 6, 6, 0.04, 180, 118, 0, 1)
        super(WateringCan, self).__init__(self.idle_image)
        self.water_anim = util.setup_animation('watering_can_water_tiled.png', 2, 6, 0.04, 180, 118, 0, 1)
        self.image_anchor = 34,20
        
        # Target bed
        self.target = None
        
        # Use sounds
        self.start_sound = util.load_sound('water_start.ogg')
        self.use_sound = util.load_sound('water_run.ogg')
        
    def use(self, bed, x, y, button, modifiers):
        self.target = bed
        self.image = self.start_anim
        self.on_animation_end = self.anim_water
        self.schedule_interval(self.water, 1)
        self.start_sound.play()
        pyglet.clock.schedule_once(self.run_sound, 1.44)
        # Dispatch event
        events.EventBus().dispatch_event(events.UseToolEvent(self, x, y, name="watering_can"))
        
    def run_sound(self, dt):
        '''
        Plays the sound of the watering can
        
        @param dt: time elapsed since last update, in seconds
        @type dt: float
        '''
        self.use_sound.play(-1)
        
    def cancel(self):
        self.image = self.idle_image
        self.target = None
        self.unschedule(self.water)
        self.unschedule(self.run_sound)
        self.use_sound.stop()
        
    def water(self, dt):
        '''
        Adds water to the target bed
        
        @param dt: time elapsed since last update, in seconds
        @type dt: float
        '''
        self.target.water += 15.0
        #print "water: "+str(self.target.water)+" nutr:"+str(self.target.nutrients)
        
    def anim_water(self):
        '''
        Starts the watering animation
        '''
        self.image = self.water_anim
        
    def get_cursor(self):
        return director.interpreter_locals["director"].window.get_system_mouse_cursor(pyglet.window.BaseWindow.CURSOR_CROSSHAIR)
    
    def get_mouse_info_image(self):
        return self